package com.xinghou.platform.dao.main.aspect;

import com.google.gson.reflect.TypeToken;
import com.xinghou.platform.dao.main.common.DBAreaCodeCached;
import com.xinghou.platform.model.main.AreaCode;
import com.xinghou.platform.model.main.DataConfig;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.util.JsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.awt.geom.Area;
import java.util.List;

/**
 * Created by qingshengzheng on 16/7/11.
 */
@Aspect
@Component
public class AreaCodeCacheAspect extends DBAreaCodeCached {
    @Pointcut("execution(public * com.xinghou.platform.dao.main.AreaCodeDAO.queryByAdcode(..))")
    public void queryByAdcode() {
    }
    @Around("queryByAdcode()")
    public AreaCode queryByAdcode(ProceedingJoinPoint pjp) throws Throwable {
        String aracode = ((Object[]) pjp.getArgs())[0].toString();
        String key = String.format(CACHE_AREA_CODE, aracode);
        Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
        if(jsonToken == null){
            AreaCode areaCode= (AreaCode)pjp.proceed();
            if(areaCode != null)
                getRedisClient().set(key, JsonUtil.toJson(areaCode), CACHED_DB_INDEX);
            return areaCode;
        }else{
            AreaCode areaCode = (AreaCode)JsonUtil.fromJson(jsonToken.toString(), AreaCode.class);
            return areaCode;
        }
    }
}
